#!/usr/bin/php -q
<?php
	/*
	 * This file is part of phpitter.
 	 * 
	 * phpitter is free software: you can redistribute it and/or modify
	 * it under the terms of the GNU General Public License as published by
	 * the Free Software Foundation, either version 3 of the License, or
	 * (at your option) any later version.
 	 * 
	 * phpitter is distributed in the hope that it will be useful,
	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	 * GNU General Public License for more details.
 	 * 
	 * You should have received a copy of the GNU General Public License
	 * along with phpitter.  If not, see <http://www.gnu.org/licenses/>.
	 *
	 * Copyright 2008 Ryan Hadley
	 */

	error_reporting(E_ERROR);
	$baseDir = dirname(__FILE__).'/';
	require_once($baseDir.'phpTwitter.class.php');
	
	$log =& phpTwitter::getLogger('phpitter_get_xml');

	$log->debug("reading from stdin ...");
	$fp = fopen('php://stdin', 'r');
	$data = unserialize(trim(fgets($fp, 255)));
	$log->debug("done reading from stdin ...");//DEBUG

	if ( $data['username'] == '' OR $data['password'] == '' OR $data['type'] == '')
	{
		die(null);
	}

	$phpTwitter = new phpTwitter($data['username'], $data['password']);
	$phpTwitter->offline = false;
	$phpTwitter->loadTweets();

	$log->debug("Just before switch statement");//DEBUG
	switch ( $data['type'] )
	{
		case 'replies':
			$updates = $phpTwitter->getTwitterXML($phpTwitter->repliesURL);
			break;
		case 'friends':
			$updates = $phpTwitter->getTwitterXML($phpTwitter->friendsTimelineURL);
			break;
		case 'directs':
			$updates = $phpTwitter->getDirectMessages($phpTwitter->directMessagesURL);
			break;
		case 'directs_sent':
			$updates = $phpTwitter->getDirectMessages($phpTwitter->directMessagesSentURL);
			break;
		default:
			die(null);
			break;
	}
	$log->debug("Just AFTER switch statement");//DEBUG

	$log->debug("About to echo serialize(\$updates) .. count(\$updates)=".count($updates['xml']) . ' ' . count($updates['ids']));
	echo serialize($updates);
	$log->debug("Just after echoing the serialized updates with FLUSH calls");
?>
